package com.jsu.hotelmanagement.observerModel.observer;

import lombok.Data;

import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

@Data
public class RoomObserver implements Delayed {

    private Integer roomId;
    private Integer orderId;
    private Date endTime;

    public RoomObserver() {
    }

    public RoomObserver(Integer roomId, Date endTime) {
        this.roomId = roomId;
        this.endTime = endTime;
    }

    public RoomObserver(Integer roomId, Integer orderId, Date endTime) {
        this.roomId = roomId;
        this.orderId = orderId;
        this.endTime = endTime;
    }

    @Override
    public String toString() {
        return "RoomObserver{" +
                "roomId=" + roomId +
                ", orderId=" + orderId +
                ", endTime=" + endTime +
                '}';
    }

    public Integer getRoomId() {
        return roomId;
    }

    public void setRoomId(Integer roomId) {
        this.roomId = roomId;
    }

    public Integer getOrderId() {
        return orderId;
    }

    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(endTime.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        if(o == null || ! (o instanceof RoomObserver)) {
            return 1;
        }
        if(o == this) {
            return 0;
        }
        RoomObserver roomObserver = (RoomObserver) o;
        return endTime.getTime() - roomObserver.endTime.getTime() > 0 ? 1 :(endTime.getTime() - roomObserver.endTime.getTime() == 0 ? 0 : -1);
    }
}
